const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const interfaces = require('os').networkInterfaces();
const path = require('path')
app.use(express.static(path.join(__dirname, 'public')))


const testUrl = require("./routers/testUrl/testUrl.js");
const uploadImg = require("./routers/uploadImg/uploadImg.js");


var IPAdress = '';
for (var devName in interfaces) {
    var iface = interfaces[devName];
    for (var i = 0; i < iface.length; i++) {
        var alias = iface[i];
        if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
            IPAdress = alias.address;
        }
    }
}

console.log(IPAdress);
//设置跨域请求头
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});



app.use(bodyParser.urlencoded({
    extended: true
}));

app.get('/',function (req,res) {
    res.json(200,`服务启动于http://${IPAdress}:8000`);
})

app.post('/index', function (req, res) {
    res.send({
        status: 200,
        data: {
            num: 1
        }
    });
});
// test
app.use('/test', testUrl);
// uploadImg
app.use('/uploadImg', uploadImg);



const server = () => app.listen("8000", function () {
    console.log(`服务启动于http://${IPAdress}:8000`);
});
server();